Skip to content

feat: add debounce and throttle utilities - #6

Closed
benaiad wants to merge 2 commits into
mainfrom
test/npm-link-verify
Closed

feat: add debounce and throttle utilities#6
benaiad wants to merge 2 commits into
mainfrom
test/npm-link-verify

Conversation

@benaiad

@benaiad benaiad commented Apr 27, 2026

Copy link
Copy Markdown
Owner

Testing npm link workflow.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR adds two simple but functional debounce and throttle utilities. The logic is correct for the happy path, but the implementation uses any types where the project convention (see src/utils/package-root.ts) avoids them entirely, and the new file has zero test coverage in a project that maintains 22 test files. Risk level: low-medium — the functions are small and isolated, but untyped internals and missing tests leave edge cases (negative/NaN delays, cross-platform timer types) unprotected.

3 finding(s): 0 error, 2 warning, 1 info

[WARNING] style

File: src/utils/debounce.ts:2

let timer: any uses a bare any type. The adjacent utility package-root.ts avoids any entirely, so this departs from project convention. It also masks the actual type returned by setTimeout, which differs between Node (NodeJS.Timeout) and browser (number) runtimes.

Suggestion: Replace let timer: any with let timer: ReturnType<typeof setTimeout> | undefined; and initialize it as undefined so clearTimeout on first call is explicitly safe.


[WARNING] testing

File: src/utils/debounce.ts

No tests exist for debounce or throttle. The project has 22 test files covering other modules. These utilities have non-trivial edge cases: timer cleanup, rapid successive calls, and boundary timing.

Suggestion: Add a test file (e.g. test/utils/debounce.test.ts) covering: (1) debounce delays execution and only fires the last call, (2) throttle limits calls within the interval, (3) debounce resets the timer on re-entry, (4) behavior with zero or negative delay values.


[INFO] error-handling

File: src/utils/debounce.ts:1

delayMs and intervalMs are not validated. Passing NaN or a negative number causes setTimeout to fire immediately and throttle to execute on every call, which may surprise callers.

Suggestion: Consider guarding with if (!Number.isFinite(delayMs) || delayMs < 0) throw new RangeError('delayMs must be a non-negative finite number') (similarly for intervalMs), or document the behavior in JSDoc.


Reviewed by pi-relay · View full run

Comment thread src/utils/debounce.ts Outdated
@@ -0,0 +1,18 @@
export function debounce<T extends (...args: any[]) => void>(fn: T, delayMs: number): T {
let timer: any;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[WARNING] style

let timer: any uses a bare any type. The adjacent utility package-root.ts avoids any entirely, so this departs from project convention. It also masks the actual type returned by setTimeout, which differs between Node (NodeJS.Timeout) and browser (number) runtimes.

Suggestion: Replace let timer: any with let timer: ReturnType<typeof setTimeout> | undefined; and initialize it as undefined so clearTimeout on first call is explicitly safe.

Comment thread src/utils/debounce.ts
@@ -0,0 +1,18 @@
export function debounce<T extends (...args: any[]) => void>(fn: T, delayMs: number): T {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[INFO] error-handling

delayMs and intervalMs are not validated. Passing NaN or a negative number causes setTimeout to fire immediately and throttle to execute on every call, which may surprise callers.

Suggestion: Consider guarding with if (!Number.isFinite(delayMs) || delayMs < 0) throw new RangeError('delayMs must be a non-negative finite number') (similarly for intervalMs), or document the behavior in JSDoc.

@github-actions

Copy link
Copy Markdown

AI Review: Fixes Applied

Reviewed and found 3 issue(s). All addressed.

Changes Made

Changes made:

  • src/utils/debounce.ts: Replaced let timer: any with let timer: ReturnType<typeof setTimeout> | undefined (addresses style warning about bare any type)
  • test/utils/debounce.test.ts: Created new test file with 6 tests covering debounce and throttle behavior (addresses testing warning about missing tests)

Skipped:

  • Info finding about input validation (NaN/negative delayMs) - not addressed per instructions

Verification passed.


View full run

@github-actions
github-actions Bot dismissed their stale review April 27, 2026 07:36

Superseded by new review.

@benaiad

benaiad commented Apr 27, 2026

Copy link
Copy Markdown
Owner Author

Test PR — verified npm link workflow. Closing.

@benaiad benaiad closed this Apr 27, 2026
@benaiad
benaiad deleted the test/npm-link-verify branch April 27, 2026 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant